﻿using System;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Collections.Generic;
using Hanselman.Portable.Controls;
using ig.app.Views;

namespace Hanselman.Portable.Views
{
    public sealed class RootPage : MasterDetailPage
    {


        public static bool IsUWPDesktop { get; set; }
        Dictionary<MenuType, NavigationPage> Pages { get; set; }
        public RootPage()
        {

            Pages = new Dictionary<MenuType, NavigationPage>();
            Master = new MenuPage(this);
            BindingContext = new BaseViewModel
            {
                Title = "Hanselman",
                Icon = "slideout.png"
            };
             NavigateAsync(MenuType.About).ConfigureAwait(false);
            InvalidateMeasure(); 

        }
        //admin teacher student
        private bool[] loadMenu = new[] { false, false, false };

        public async Task NavigateAsync(MenuType id)
        {

            if (Detail != null)
            {
                if (IsUWPDesktop || Device.Idiom != TargetIdiom.Tablet)
                    IsPresented = false;

                if (Device.OS == TargetPlatform.Android)
                    await Task.Delay(300);
            } 

            NavigationPage newPage = null;
            if (!Pages.ContainsKey(id))
            {
                switch (id)
                {
                    case MenuType.About:
                        newPage = new HanselmanNavigationPage(new AboutPage());
                        break;
                    case MenuType.Setting:
                        newPage = new HanselmanNavigationPage(new SettingPage());
                        break;
                    case MenuType.Answer:
                        newPage = new HanselmanNavigationPage(new AnswerPage());
                        break;
                    case MenuType.Homework:
                        newPage = new HanselmanNavigationPage(new HomeworkPage());
                        break;
                }
                Pages.Add(id, newPage);
            }
            else
            {
                newPage = Pages[id];
            }
            if (newPage == null)
                return;

            //pop to root for Windows Phone
            if (Detail != null && Device.OS == TargetPlatform.WinPhone)
            {
                await Detail.Navigation.PopToRootAsync();
            }

            Detail = newPage;
        }
    }
}

